package javasec.contempt.visuals;

import java.awt.Frame;
import java.awt.Panel;
import java.awt.BorderLayout;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.EditorPart;

import processing.core.PApplet;


public class ExampleProcessingVisual extends EditorPart
{
	//public static String ID = "javasec.contempt.visuals.layer2editor";
	public static String ID = "javasec.contempt.visuals.ExampleProcessingVisual";
	//private Text entry;

	public class MyProcessingSketch extends PApplet
	{
//		An array of stripes
		Stripe[] stripes = new Stripe[50];

		public void setup()
		{
			size(200,200);
			// Initialize all "stripes"
			for (int i = 0; i < stripes.length; i++)
			{
				stripes[i] = new Stripe(this);
			}
		}

		public void draw()
		{
			background(100);
			// Move and display all "stripes"
			for (int i = 0; i < stripes.length; i++)
			{
				stripes[i].move();
				stripes[i].display();
			}
		}
	}
	
	public class Stripe
	{
		float x;       // horizontal location of stripe
		float speed;   // speed of stripe
		float w;       // width of stripe
		boolean mouse; // state of stripe (mouse is over or not?)
		PApplet parent; // The parent PApplet that we will render ourselves onto

		Stripe(PApplet p)
		{
			parent = p;
			x = 0;              // All stripes start at 0
			speed = parent.random(1);  // All stripes have a random positive speed
			w = parent.random(10,30);
			mouse = false;
		}

		// Draw stripe
		void display()
		{
			parent.fill(255,100);
			parent.noStroke();
			parent.rect(x,0,w,parent.height);
		}

		// Move stripe
		void move()
		{
			x += speed;
			if (x > parent.width+20) x = -20;
		}
	}
	
	public ExampleProcessingVisual()
	{
		// TODO Auto-generated constructor stub
	}

	public void doSave(IProgressMonitor monitor)
	{
		// TODO Auto-generated method stub

	}

	public void doSaveAs()
	{
		// TODO Auto-generated method stub

	}

	public void init(IEditorSite site, IEditorInput input)
			throws PartInitException
	{
		System.out.println("new processing init()ed");
		// TODO Auto-generated method stub
		setSite(site);
		setInput(input);
		setPartName("Processing Example");
	}

	public boolean isDirty()
	{
		// TODO Auto-generated method stub
		return false;
	}

	public boolean isSaveAsAllowed()
	{
		// TODO Auto-generated method stub
		return false;
	}
	
	public void createPartControl(Composite parent)
	{
		try
		{
			
		Composite top = new Composite(parent, SWT.EMBEDDED); 
		Frame frame = SWT_AWT.new_Frame(top);

		Panel panel;
		panel = new Panel(new BorderLayout())
		{ 
			public void update(java.awt.Graphics g)
			{
				/* Do not erase the background */ 
				paint(g); 
			}
		};

		MyProcessingSketch sketch=new MyProcessingSketch();
		
		frame.add(panel);
		panel.add(sketch);
		
		sketch.init();
		
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		
		System.out.println("that was fun");
	}

	public void setFocus()
	{
		System.out.println("focusing editor");
		// TODO Auto-generated method stub

	}

}
